package com.cyl.XmlDemo.web;

import java.util.List;

import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cyl.XmlDemo.entity.DataResult;
import com.cyl.XmlDemo.entity.Register;
import com.cyl.XmlDemo.entity.ShiroPrincipal;
import com.cyl.XmlDemo.entity.User;
import com.cyl.XmlDemo.service.UserService;
import com.cyl.XmlDemo.utils.ShiroUtils;

/**
 * 用户控制器
 * @author tyuio
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	private final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/test")
	@ResponseBody
	public DataResult test(DataResult dr){
		logger.debug("测试啦啦啦啦啦啦");
		dr.setCode("112");
		return dr;
	}
	
	@RequestMapping(value="/login")
	@ResponseBody
	public DataResult login(User user,DataResult dr){
		logger.debug("login");
		userService.login(user);
		ShiroPrincipal shiroPrincipal = ShiroUtils.getPrincipal();
		User u = shiroPrincipal.getUser();
		u.setPassword(null);
		dr.setCode("success");
		dr.setData(u);
		
		return dr;
		
	}
	
	@RequestMapping(value="/register",method=RequestMethod.POST)
	@ResponseBody
	public DataResult register(DataResult dr,Register register){
		logger.debug("注册");
		if(register.getName() == null || "".equals(register.getName())){
			dr.setCode("error-username-null");
			dr.setMsg("用户名不能为空");
			return dr;
		}
		if(register.getUsername() == null || "".equals(register.getUsername())){
			dr.setCode("error-name-null");
			dr.setMsg("账号不能为空");
			return dr;
		}
		if(register.getPassword() == null || "".equals(register.getPassword())){
			dr.setCode("error-password-null");
			dr.setMsg("密码不能为空");
			return dr;
		}
		if(register.getRepassword() == null || "".equals(register.getRepassword())){
			dr.setCode("error-repassword-null");
			dr.setMsg("确认密码不能为空");
			return dr;
		}
		if(!register.getPassword().equals(register.getRepassword())){
			dr.setCode("error-password-twice");
			dr.setMsg("两次输入的密码错误请重新输入");
			return dr;
		}
		String code = userService.register(register);
		dr.setCode(code);
		return dr;
	}
	
	@RequestMapping(value="/logout",method=RequestMethod.POST)
	@ResponseBody
	public DataResult logout(DataResult dr){
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		dr.setCode("success");
		return dr;
	}
	
	@RequestMapping(value="/status",method=RequestMethod.POST)
	@ResponseBody
	public DataResult status(DataResult dr){
//		Subject subject = SecurityUtils.getSubject();
		ShiroPrincipal shiroPrincipal = ShiroUtils.getPrincipal();
		if(shiroPrincipal == null){
			dr.setCode("offline");
		}else{
			dr.setCode("online");
			User u = shiroPrincipal.getUser();
			u.setPassword(null);
			dr.setData(u);
		}
		return dr;
	}
}
